//
//  CCKSubMainHeadCell.m
//  BaiSiBuDeJie
//
//  Created by SuperJJ on 16/8/31.
//  Copyright © 2016年 tsaievan. All rights reserved.
//

/**
 *  1.图片的适配
 *  2.内容的显示
 *  3.帖子数和订阅人数
 *  4.分享按钮
 *  5.帖子数等的阴影
 *  6.图片的阴影
 */

#import "CCKSubMainHeadCell.h"
#import "CCKSubMainHeadModel.h"


@interface CCKSubMainHeadCell ()

@property (nonatomic, weak) UIImageView *imgView;
@property (nonatomic, weak) UILabel     *infoLabel;
@property (nonatomic, weak) UIButton    *shareButton;
@property (nonatomic, weak) UILabel     *subPostNumLabel;

@end

@implementation CCKSubMainHeadCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI
{
    __weak typeof(self) weakSelf = self;
    
    
    // -------- imgView --------
    UIImageView * imgView = [[UIImageView alloc] init];
    self.imgView = imgView;
    [self.contentView addSubview:imgView];
    imgView.userInteractionEnabled = YES;
    
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.right.top.mas_equalTo(weakSelf.contentView);
         make.height.mas_equalTo(kScreenW * 300 / 640.0);
     }];

    
    // -------- infoLabel --------
    UILabel *infoLabel = [UILabel new];
    self.infoLabel = infoLabel;
    [self.contentView addSubview:infoLabel];
    infoLabel.numberOfLines = 0;
    infoLabel.font = [UIFont systemFontOfSize:14];
    infoLabel.textColor = [UIColor grayColor];
    
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(weakSelf.contentView).offset(8);
        make.right.equalTo(weakSelf.contentView).offset(-8);
        make.bottom.equalTo(weakSelf.contentView).offset(-2);
        make.top.equalTo(imgView.mas_bottom).offset(2);
    }];
    
    // -------- shareButton --------
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _shareButton = shareButton;
    [imgView addSubview:shareButton];
    [shareButton setImage:[UIImage imageNamed:@"tag_share_post_tag"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageNamed:@"tag_share_post_tag_click"] forState:UIControlStateHighlighted];
    [shareButton sizeToFit];
    [shareButton addTarget:self action:@selector(shareBtnAction:) forControlEvents:UIControlEventTouchUpInside];

    [shareButton mas_remakeConstraints:^(MASConstraintMaker *make)
    {
        make.right.equalTo(imgView).offset(-10);
        make.bottom.equalTo(imgView).offset(-10);
    }];
    
    
    // -------- subPostNumLabel --------
    UILabel *subPostNumLabel = [UILabel new];
    self.subPostNumLabel = subPostNumLabel;
    [imgView addSubview:subPostNumLabel];
    subPostNumLabel.numberOfLines = 2;
    _subPostNumLabel.textColor = [UIColor whiteColor];
    _subPostNumLabel.shadowColor = [UIColor blackColor];
    _subPostNumLabel.shadowOffset = CGSizeMake(1, 1);
    [_subPostNumLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.5]];
    
    [subPostNumLabel mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(imgView).offset(6);
        make.bottom.equalTo(imgView).offset(-10);
    }];
    

}

- (void)shareBtnAction:(UIButton *)sender
{
    NSLog(@"点击");
}

- (void)setModel:(CCKSubMainHeadModel *)model
{
    _model = model;
    
    // -------- imgView --------
    [_imgView sd_setImageWithURL:[NSURL URLWithString:model.image_detail] placeholderImage:[UIImage imageNamed:@"post_tag_default_big_icon"]];
    
    //NSLog(@"%@",model.image_detail);

    // -------- infoLabel --------
    if ([model.info isEqualToString:@""] == YES)
    {
        __weak typeof(self) weakSelf = self;
        [_infoLabel mas_remakeConstraints:^(MASConstraintMaker *make)
        {
            make.left.bottom.equalTo(weakSelf.contentView);
            make.top.equalTo(_imgView.mas_bottom);
          
        }];
    }else
    {
        _infoLabel.text = model.info;
    }

    // -------- subPostNumLabel --------
     NSAttributedString *attrPostNumString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", model.post_number] attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.00 green:0.69 blue:0.02 alpha:1.00]}];
    
    NSAttributedString *attrSubNumString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", model.sub_number] attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.00 green:0.69 blue:0.02 alpha:1.00]}];
    
    NSMutableAttributedString *mtAttrString = [[NSMutableAttributedString alloc] initWithString:@"帖子数: "];
    [mtAttrString appendAttributedString:attrPostNumString];
    [mtAttrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\r\n订阅人数: "]];
    [mtAttrString appendAttributedString:attrSubNumString];
    
    [_subPostNumLabel setAttributedText:mtAttrString];
}







@end
